Einführung in R Markdown

Stefan Zimmermann

02.07.2021

Dynamische Dokumente

logo

Bieten die Möglichkeit computerbasierte Sprache und lesbaren Text zu kombinieren.

Beispiele für dynamische Dokumente:

Jupyter Notebooks, dyndoc(STATA) & Rmarkdown

Vorteile:

Kombination verschiedener Technologien:

logo

Wie wird ein dynamisches R Markdown Dokument erzeugt?

Anlegen und rendern eines R Markdown-Dokumentes

logo

File anlegen

R Markdown rendern

Aufbau eines R Markdown Dokumentes

logo

Output Formate

logo

YAML-Metadaten:

logo

Anpassen der YAML-Metadaten:

- Ein Inhaltsverzeichnis kann auch unterschiedlich angeordnet werden zum Beispiel als fließendes Inhaltsverzeichnis in einer html.

Syntax: Markdown

logo

Text Formatierung

Überschriften

Sytax: R-Chunks

logo

Befehl Beschreibung
Globale Optionen
eval Code auswerten
echo Code anzeigen
warning Warnung anzeigen
message Nachricht anzeigen
error Fehler anzeigen
Grafik Optionen
fig.width/fig.height Die Größe der Grafik in inches
out.width/out.height Ausgabegröße der Grafik im Dokument in Prozent
fig.align Ausrichtung der Grafik
fig.cap Überschrift der Grafik
knitr::opts_chunk$set(fig.width = 8, collapse = TRUE)
knitr::opts_chunk$set(engine.path = list(
                      python = 'C:/Users/Stefan/AppData/Local/Programs/Python/Python36/python',
                      stata = 'C:/git/stata15/StataSE.exe')
num = 8 
num_sqrt = num ** 0.5
print('The square root of %0.3f is %0.3f'%(num ,num_sqrt))
## The square root of 8.000 is 2.828
library(Statamarkdown)
# stataexe <- find_stata()
knitr::opts_chunk$set(engine.path=list(stata="C:/git/stata15/StataSE.exe"),
  error=TRUE, cleanlog=TRUE, comment=NA)
sysuse auto, clear
generate gpm = 1/mpg
summarize price gpm
(1978 Automobile Data)

    Variable |        Obs        Mean    Std. Dev.       Min        Max
-------------+---------------------------------------------------------
       price |         74    6165.257    2949.496       3291      15906
         gpm |         74    .0501928    .0127986   .0243902   .0833333

Tabellen mit kable

logo

data <- read.csv("C:/Users/Stefan/Documents/IPSD/data_vis/week_3/footballdata.csv" ,
                 header=TRUE, sep=",", encoding = "UTF-8")

data_filtered <- data %>%
    group_by(Position, League) %>%
    summarise(avg_attack = round(mean(Attack, na.rm = T), 2)) %>%
    filter(!is.na(Position))

knitr::kable(data_filtered) %>%
       kable_styling("striped", full_width = F) %>%
       column_spec(2, bold = T) %>%
       pack_rows("Verteidiger", 1, 4, 
            label_row_css = "background-color: #666; color: #fff;") %>%
       pack_rows("Stürmer", 5, 8, 
            label_row_css = "background-color: #666; color: #fff;") %>%
       pack_rows("Torhüter", 9, 12, 
            label_row_css = "background-color: #666; color: #fff;") %>%
       pack_rows("Mittelfeld", 13, 16, 
            label_row_css = "background-color: #666; color: #fff;") %>%
       row_spec(c(1,8,11,14), bold = T, color = "white", background = "green")
Position League avg_attack
Verteidiger
Defender Bundesliga 66.60
Defender LaLiga 65.96
Defender PremierLeague 66.43
Defender SerieA 66.34
Stürmer
Forward Bundesliga 80.23
Forward LaLiga 80.41
Forward PremierLeague 80.22
Forward SerieA 80.64
Torhüter
Goalkeeper Bundesliga 30.22
Goalkeeper LaLiga 30.23
Goalkeeper PremierLeague 30.56
Goalkeeper SerieA 30.17
Mittelfeld
Midfielder Bundesliga 73.59
Midfielder LaLiga 73.89
Midfielder PremierLeague 73.39
Midfielder SerieA 73.74
data_filtered %>%
    mutate_if(is.numeric, function(x) {
    cell_spec(x, bold = T,
    color = spec_color(x, end = 0.9),
    font_size = spec_font_size(x))}) %>%

    mutate(League = cell_spec(League, color = "white", 
                              bold = T, 
                              background = spec_color(1:4, end = 0.9, option = "A", 
                                                      direction = -1))) %>%
  
    kable(escape = F, align = "c") %>%
    kable_styling(c("striped", "condensed"), full_width = F)
Position League avg_attack
Defender Bundesliga 66.6
Defender LaLiga 65.96
Defender PremierLeague 66.43
Defender SerieA 66.34
Forward Bundesliga 80.23
Forward LaLiga 80.41
Forward PremierLeague 80.22
Forward SerieA 80.64
Goalkeeper Bundesliga 30.22
Goalkeeper LaLiga 30.23
Goalkeeper PremierLeague 30.56
Goalkeeper SerieA 30.17
Midfielder Bundesliga 73.59
Midfielder LaLiga 73.89
Midfielder PremierLeague 73.39
Midfielder SerieA 73.74
# install.packages('DT')
datatable(data_filtered)

Mathematische Ausdrücke und Formeln:

logo

$$var_{\bar{y}_{w}} = \sum_{h=1}^{5} w_{h}^2*(1-f_{h}/n_{h})*var_{\bar{y}_{h}}$$

\[var_{\bar{y}_{w}} = \sum_{h=1}^{5} w_{h}^2*(1-f_{h}/n_{h})*var_{\bar{y}_{h}}\]

N <- 1000
N1 <- 123
nh <- 66
f <- 8/N1
stratum1_variance <- 33.453

# $$var_{(\bar{y}_{w})_{1}}  = (`r N1`/`r N`)^2 * (1-(`r f`))/`r nh` * `r stratum1_variance`$$
# $$var_{(\bar{y}_{w})_{1}}  = `r (N1/N)^2` * `r (1-(f))/nh * stratum1_variance`$$
# $$var_{(\bar{y}_{w})_{1}}  = `r (N1/N)^2 * (1-(f))/nh * stratum1_variance`$$

\[var_{(\bar{y}_{w})_{1}} = (123/1000)^2 * (1-(0.0650407))/66 * 33.453\] \[var_{(\bar{y}_{w})_{1}} = 0.015129 * 0.4738969\]

\[var_{(\bar{y}_{w})_{1}} = 0.0071696\]

Grafiken:

logo - Grafiken mit Base R oder ggplot sind möglich.

# bullet
data_filtered <- data %>%
  filter(Position == "Goalkeeper")

data_filtered <- subset(data_filtered, select=c(Name, KeeperSkills))

fig <- plot_ly() 
fig <- fig %>%
  add_trace(
    type = "indicator",
    mode = "number+gauge+delta",
    value = data_filtered[34,2],
    delta = list(reference = 85),
    domain = list(x = c(0.25, 1), y = c(0.1, 0.3)),
    title =list(text = paste0(data_filtered[34,1])),
    gauge = list(
      shape = "bullet",
      axis = list(range = c(0, 100)),
      threshold = list(
        line= list(color = "black", width = 2),
        thickness = 0.75,
        value = 85),
      steps = list(
        list(range = c(0, 50), color = "#fc9272"),
        list(range = c(50, 80), color = "#ffeda0"),
        list(range = c(80, 100), color = "#a1d99b")),
      bar = list(color = "black")))
fig <- fig %>%
  add_trace(
    type = "indicator",
    mode = "number+gauge+delta",
    value = data_filtered[19,2],
    delta = list(reference = 85),
    domain = list(x = c(0.25, 1), y = c(0.4, 0.6)),
    title =list(text = paste0(data_filtered[19,1])),
    gauge = list(
      shape = "bullet",
      axis = list(range = c(0, 100)),
      threshold = list(
        line= list(color = "black", width = 2),
        thickness = 0.75,
        value = 85),
      steps = list(
        list(range = c(0, 50), color = "#fc9272"),
        list(range = c(50, 80), color = "#ffeda0"),
        list(range = c(80, 100), color = "#a1d99b")),
      bar = list(color = "black"))) 
fig <- fig %>%
  add_trace(
    type = "indicator",
    mode = "number+gauge+delta",
    value = data_filtered[173,2],
    delta = list(reference = 85),
    domain = list(x = c(0.25, 1), y = c(0.7, 0.9)),
    title =list(text = paste0(data_filtered[173,1])),
    gauge = list(
      shape = "bullet",
      axis = list(range = c(0, 100)),
      threshold = list(
        line= list(color = "black", width = 2),
        thickness = 0.75,
        value = 85),
      steps = list(
        list(range = c(0, 50), color = "#fc9272"),
        list(range = c(50, 80), color = "#ffeda0"),
        list(range = c(80, 100), color = "#a1d99b")),
      bar = list(color = "black"))) 
fig
data_filtered <- data %>%
  group_by(Position, League) %>%
  filter(Position != "Goalkeeper") %>%
  summarise(avg_attack = mean(DribbleSpeed, na.rm = T)) %>%
  filter(!is.na(Position))

DribbleSpeed <- ggplotly(ggplot() +
  geom_line(aes(League, avg_attack, colour = Position, group = Position),
            data = data_filtered, size=2) +
  theme_bw() +
  theme(axis.title.x=element_blank(),
        axis.title.y=element_blank())+
  ylim(70,100)+
  labs(x = "League", y = "Mean DribbleSpeed Score", color = "Position",
       title = "Average DribbleSpeed Score by League and Position",
       caption = "Data: footballdata"))

DribbleSpeed

Batch-Reports

logo

rmarkdown::render("C:/git/gitHub/rmarkdown_praesentation/output/league_report.Rmd", 
                  output_file = "C:/git/gitHub/rmarkdown_praesentation/output/LaLiga_report.pdf",
                  params = list(nms = "LaLiga"))


processing file: league_report.Rmd
output file: league_report.knit.md

Output created: output/LaLiga_report.pdf

library(purrr)

render_report <- function(path, league) {
  rmarkdown::render(
    path,
    params = list(nms = league),
    output_file = paste0("C:/git/gitHub/rmarkdown_praesentation/output/", 
                         league, "_report.pdf")
  )
}

map(c("Bundesliga", "PremierLeague", "LaLiga", "SerieA"),
    render_report,
    path = "C:/git/gitHub/rmarkdown_praesentation/output/league_report.Rmd")


processing file: league_report.Rmd
output file: league_report.knit.md

Output created: output/Bundesliga_report.pdf


processing file: league_report.Rmd
output file: league_report.knit.md

Output created: output/PremierLeague_report.pdf


processing file: league_report.Rmd
output file: league_report.knit.md

Output created: output/LaLiga_report.pdf


processing file: league_report.Rmd
output file: league_report.knit.md

Output created: output/SerieA_report.pdf

Vielen Dank für die Aufmerksamkeit